/**
 * 创建 Virtual DOM
 * @param {string} type - 类型
 * @param {object | null} props - 属性
 * @param  {createElement[]} children - 子元素
 * @returns {object} - Virtual DOM
 */
export default function createElement(type, props, ...children) {
  const childElements = [].concat(children).reduce((result, child) => {
    if (child !== false && child !== true && child !== null) {
      if (Object.prototype.toString.call(child).slice(8, -1) === 'Object') {
        result.push(child);
      } else if (
        Object.prototype.toString.call(child).slice(8, -1) === 'Array'
      ) {
        result.push(...child);
      } else {
        result.push(createElement('text', { textContent: child }));
      }
    }
    return result;
  }, []);

  return {
    type,
    props: Object.assign({ children: childElements }, props),
  };
}
